//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

use proto_gen_rust::board_chat_api::board_chat_api_client::BoardChatApiClient;
use proto_gen_rust::board_chat_api::*;
use tauri::{AppHandle, Emitter, Runtime};

use crate::notice_decode::new_wrong_session_notice;

#[tauri::command]
pub async fn board_chat_api_cmds_send<R: Runtime>(
    app_handle: AppHandle<R>,
    request: SendRequest,
) -> Result<SendResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardChatApiClient::new(chan.unwrap());
    match client.send(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == send_response::Code::WrongSession as i32 {
                if let Err(err) = &app_handle.emit(
                    "notice",
                    new_wrong_session_notice("send".into()),
                ) {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_chat_api_cmds_list_after<R: Runtime>(
    app_handle: AppHandle<R>,
    request: ListAfterRequest,
) -> Result<ListAfterResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardChatApiClient::new(chan.unwrap());
    match client.list_after(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == list_after_response::Code::WrongSession as i32 {
                if let Err(err) = &app_handle.emit(
                    "notice",
                    new_wrong_session_notice("list_after".into()),
                ) {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_chat_api_cmds_list_before<R: Runtime>(
    app_handle: AppHandle<R>,
    request: ListBeforeRequest,
) -> Result<ListBeforeResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardChatApiClient::new(chan.unwrap());
    match client.list_before(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == list_before_response::Code::WrongSession as i32 {
                if let Err(err) = &app_handle.emit(
                    "notice",
                    new_wrong_session_notice("list_before".into()),
                ) {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_chat_api_cmds_get_unread_count<R: Runtime>(
    app_handle: AppHandle<R>,
    request: GetUnreadCountRequest,
) -> Result<GetUnreadCountResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardChatApiClient::new(chan.unwrap());
    match client.get_unread_count(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == get_unread_count_response::Code::WrongSession as i32 {
                if let Err(err) = &app_handle.emit(
                    "notice",
                    new_wrong_session_notice("get_unread_count".into()),
                ) {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_chat_api_cmds_clear_unread<R: Runtime>(
    app_handle: AppHandle<R>,
    request: ClearUnreadRequest,
) -> Result<ClearUnreadResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardChatApiClient::new(chan.unwrap());
    match client.clear_unread(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == clear_unread_response::Code::WrongSession as i32 {
                if let Err(err) = &app_handle.emit(
                    "notice",
                    new_wrong_session_notice("clear_unread".into()),
                ) {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}